package com.viish.apps.tripletriad.carte;

import com.viish.apps.tripletriad.views.CompleteCardView;

public class Card
{
	private String name;
	private String element;
	private int level, edition, number;
	private String top, left, bot, right;
	private boolean locked = false, selected = false;
	private CompleteCardView view;
	
	public Card(String name, int level, int edition, String top, String left, String bot, String right, String element, CompleteCardView cv, int number)
	{
		this.name = name;
		this.level = level;
		this.edition = edition;
		this.top = top;
		this.left = left;
		this.bot = bot;
		this.right = right;
		this.element = element;
		this.number = number;
		this.view = cv;
	}
	
	@Override
	public Card clone()
	{
		Card clone = new Card(name, level, edition, top, left, bot, right, element, null, number);
		clone.setSelected(selected);
		
		if (locked)
			clone.lock();
		else
			clone.unlock();
		
		return clone;
	}
	
	public int getTotal()
	{
		return getTopValue() + getBottomValue() + getLeftValue() + getRightValue();
	}
	
	public int getNumber()
	{
		return this.number;
	}
	
	public void setNumber(int number)
	{
		this.number = number;
	}
	
	public void bonusElementaire()
	{
		if (!this.top.equals("A"))
		{
			int temp = Integer.parseInt(this.top);
			temp += 1;
			if (temp == 10) this.top = "A";
			else this.top = temp + "";
		}
		
		if (!this.left.equals("A"))
		{
			int temp = Integer.parseInt(this.left);
			temp += 1;
			if (temp == 10) this.left = "A";
			else this.left = temp + "";
		}
		
		if (!this.bot.equals("A"))
		{
			int temp = Integer.parseInt(this.bot);
			temp += 1;
			if (temp == 10) this.bot = "A";
			else this.bot = temp + "";
		}
		
		if (!this.right.equals("A"))
		{
			int temp = Integer.parseInt(this.right);
			temp += 1;
			if (temp == 10) this.right = "A";
			else this.right = temp + "";
		}
		
		if (view != null) view.bonusElementaire();
	}
	
	public void malusElementaire()
	{
		if (!this.top.equals("A"))
		{
			int temp = Integer.parseInt(this.top);
			temp -= 1;
			this.top = temp + "";
		}
		else
		{
			this.top = "9";
		}
		
		if (!this.left.equals("A"))
		{
			int temp = Integer.parseInt(this.left);
			temp -= 1;
			this.left = temp + "";
		}
		else
		{
			this.left = "9";
		}
		
		if (!this.bot.equals("A"))
		{
			int temp = Integer.parseInt(this.bot);
			temp -= 1;
			this.bot = temp + "";
		}
		else
		{
			this.bot = "9";
		}
		
		if (!this.right.equals("A"))
		{
			int temp = Integer.parseInt(this.right);
			temp -= 1;
			this.right = temp + "";
		}
		else
		{
			this.right = "9";
		}
		
		if (view != null) view.malusElementaire();
	}
	
	public CompleteCardView getView()
	{
		return this.view;
	}
	
	@Override
	public String toString()
	{
		return this.name + " (level " +  this.level + ") : " + this.top + ", " + this.left + ", " + this.bot + ", " + this.right + ", " + this.element; 
	}
	
	public void lock()
	{
		this.locked = true;
	}
	
	public void unlock()
	{
		this.locked = false;
	}
	
	public boolean isPlayed()
	{
		return this.locked;
	}
	
	public boolean isSelected()
	{
		return this.selected;
	}
	
	public void setSelected(boolean selected)
	{
		this.selected = selected;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public String getFullName()
	{
		return "ff" + this.edition + "/" + this.name;
	}
	
	public int getEdition()
	{
		return this.edition;
	}
	
	public String getElement()
	{
		return this.element;
	}
	
	public int getTopValue()
	{
		try
		{
			return Integer.parseInt(this.top);
		}
		catch (NumberFormatException e)
		{
			return 10;
		}
	}
	
	public int getLeftValue()
	{
		try
		{
			return Integer.parseInt(this.left);
		}
		catch (NumberFormatException e)
		{
			return 10;
		}
	}
	
	public int getBottomValue()
	{
		try
		{
			return Integer.parseInt(this.bot);
		}
		catch (NumberFormatException e)
		{
			return 10;
		}
	}
	
	public int getRightValue()
	{
		try
		{
			return Integer.parseInt(this.right);
		}
		catch (NumberFormatException e)
		{
			return 10;
		}
	}
	
	public int getLevel()
	{
		return this.level;
	}
}
